package com.gsq.learning.mybatisplugin.plugin.type;

import org.apache.ibatis.type.BigDecimalTypeHandler;
import org.apache.ibatis.type.MappedTypes;

import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//@MappedJdbcTypes(JdbcType.NUMERIC)
@MappedTypes({BigDecimal.class}) // 将所有的BigDecimal进行转换为四舍五入的1位小数
public class MyBigDecimalTypeHandler extends BigDecimalTypeHandler {

    @Override
    public BigDecimal getNullableResult(ResultSet rs, String columnName) throws SQLException {
        BigDecimal nullableResult = super.getNullableResult(rs, columnName);
        System.out.println("======> MyBigDecimalTypeHandler，" + nullableResult);
        return super.getNullableResult(rs, columnName).setScale(1, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        BigDecimal nullableResult = super.getNullableResult(rs, columnIndex);
        System.out.println("======> MyBigDecimalTypeHandler， " + nullableResult);
        return super.getNullableResult(rs, columnIndex).setScale(1, BigDecimal.ROUND_HALF_UP);
    }

    @Override
    public BigDecimal getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        BigDecimal nullableResult = super.getNullableResult(cs, columnIndex);
        System.out.println("======> MyBigDecimalTypeHandler，" + nullableResult);
        return super.getNullableResult(cs, columnIndex).setScale(1, BigDecimal.ROUND_HALF_UP);
    }
}
